package com.itheima.service;

import java.sql.SQLException;
import java.util.List;

import com.itheima.dao.BookDao;
import com.itheima.dao.CartDao;
import com.itheima.domain.Cart;
import com.itheima.domain.PageBean;

public class CartService {

	/**
	 * 查询所有的书籍信息
	 * @return
	 * @throws SQLException 
	 */
	public List<Cart> findAllCart(String username) throws SQLException{
		
		return new CartDao().findAllCart(username);
	}
	
	/**
	 * 添加操作
	 * @param cart
	 * @return
	 * @throws SQLException 
	 */
	public int addCart(Cart cart) throws SQLException{		
		return new CartDao().addCart(cart);
	}
	
	/**
	 * 删除操作
	 * @param cid
	 * @return
	 * @throws SQLException 
	 */
	public int deleteCart(Integer cid) throws SQLException{		
		return new CartDao().deleteCart(cid);
	}

	/**
	 *  分页查询购物车信息
	 * @param currPage
	 * @param username
	 * @return
	 * @throws SQLException 
	 */
	public PageBean<Cart> findByPage(Integer currPage,String username) throws SQLException {
		// TODO Auto-generated method stub
		PageBean<Cart> pageBean = new PageBean<Cart>();
		// 设置pageBean的参数:
		// 设置当前页数:
		pageBean.setCurrPage(currPage);
		// 设置每页显示记录数:
		int pageSize = 10;
		pageBean.setPageSize(pageSize);
		// 设置总记录数:
		CartDao cartDao = new CartDao();
		int totalCount = cartDao.findCount(username);
		pageBean.setTotalCount(totalCount);
		// 设置总页数:
		int totalPage = 0;
		if (totalCount % pageSize == 0) {
			totalPage = totalCount / pageSize;
		} else {
			totalPage = totalCount / pageSize + 1;
		}		
		pageBean.setTotalPage(totalPage);
		// 设置每页显示的数据的集合:
		int begin = (currPage - 1) * pageSize;
		List<Cart> list = cartDao.findByPage(begin, pageSize,username);
		pageBean.setList(list);
		return pageBean;
	}
	
}
